//
//  HNUserSettingViewController.m
//  HappyNews
//
//  Created by baiping on 16/1/13.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HNUserSettingViewController.h"
#import "HNUserSettingCell.h"
#import "HNLoginViewController.h"
#import "HNPersonalHomePageViewController.h"
#import <UIImageView+WebCache.h>

@interface HNUserSettingViewController ()<UITableViewDataSource, UITableViewDelegate, HNLoginViewControllerDelegate, HNPersonalHomePageViewControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) HNLoginViewController *loginVC;

@property (nonatomic, strong) HNPersonalHomePageViewController *personVC;

@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) UILabel *firstCellLabel;

@end

@implementation HNUserSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addSubviews];
    
    
    
    
    
    
}

- (void)addSubviews{
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight - 64) style:(UITableViewStyleGrouped)];
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    // 添加头视图
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    
    _headView.backgroundColor = HNBgColor;
    
    [self.view addSubview:_headView];
    
    _backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    _backButton.frame = CGRectMake(10, 22, 30, 30);
    
    // 给返回按钮添加方法
    [_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_backButton setBackgroundImage:[UIImage imageNamed:@"Field_Back"] forState:(UIControlStateNormal)];
    
    [self.view addSubview:_backButton];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth / 2 - 20, 22, 40, 30)];
    
    _titleLabel.text = @"设置";
    
    _titleLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_titleLabel];

    
}

// 实现返回按钮的返回方法
- (void)backButtonAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return 1;
            break;
            
            
        case 1:
            return 1;
            break;
            
        case 2:
            return 2;
            break;
            
            
        case 3:
            return 2;
            break;
            
            
        case 4:
            return 2;
            break;
            
            
        case 5:
            return 1;
            break;
            
        default:
            break;
    }
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identify = @"myCell";
    
    HNUserSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if (!cell) {
        
        cell = [[HNUserSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        
    }
    
    // 由于cell个数比较少 并且每个cell的布局有区别 因此进行cell的判断
    if (indexPath.section == 0) {
        
        _icon = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 30, 30)];
        
        [cell addSubview:_icon];
        
        _firstCellLabel = [[UILabel alloc]initWithFrame:CGRectMake(_icon.right + 5, _icon.top, 200, 30) ];

        [cell addSubview:_firstCellLabel];
        
        cell.mySwitch.hidden = YES;
        
        // 添加我的资料 判断当前的登录状态
        [self addUserInfomatiom];
        
        [cell.detailBtn addTarget:self action:@selector(detailBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        _icon.layer.cornerRadius = 15;
        
        _icon.layer.masksToBounds = YES;
        
        [cell.detailBtn setBackgroundImage:[UIImage imageNamed:@"Editor_Arrow"] forState:(UIControlStateNormal)];
        
        
    }else if (indexPath.section == 1){
        
       cell.detailBtn.hidden = YES;
        
       cell.mySwitch.on = YES;
        
       cell.contentLabel.text = @"自动离线下载";
        
    }else if (indexPath.section == 2){
        
        cell.detailBtn.hidden = YES;
        
        cell.mySwitch.on = NO;
        
        if (indexPath.row == 0) {
            cell.contentLabel.text = @"移动网络不下载图片";
            
        }else{
            
          cell.contentLabel.text = @"大字号";
            
        }
    }else if (indexPath.section == 3){
        
        
        cell.detailBtn.hidden = YES;
        
        cell.mySwitch.on = YES;
        
        if (indexPath.row == 0) {
            cell.contentLabel.text = @"消息推送";
            
        }else{
            
            cell.contentLabel.text = @"点评分享到微博";
        }
     
    }else if(indexPath.section == 4){
        
        
    cell.mySwitch.hidden = YES;
        
    [cell.detailBtn setBackgroundImage:[UIImage imageNamed:@"Editor_Arrow"] forState:(UIControlStateNormal)];
        
        if (indexPath.row == 0) {
            cell.contentLabel.text = @"去好评";
            
        }else{
            
            cell.contentLabel.text = @"去吐槽";
        }

    }else{
        
      cell.mySwitch.hidden = YES;
      cell.detailBtn.hidden = YES;
        
      cell.contentLabel.text = @"清楚缓存";
        
    }
    
    return cell;
    

}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
    if (section == 1) {
        
    return @"    仅Wi-Fi下可用, 自动下载最新内容";
        
  }
    
    return nil;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HNUserSettingCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selected = NO;
    
    
}

// 实现按钮点击按钮
- (void)detailBtnAction:(UIButton *)sender{
    
     BOOL isExistAccount = [[HNAccountTool shareAccountTool]isExistCurrrentAccount];
    
   
    if (!isExistAccount) {
        
        self.loginVC.view.backgroundColor = [UIColor whiteColor];
        
        _loginVC = [[HNLoginViewController alloc]init];
        
        _loginVC.delegate = self;
        
        [self.navigationController pushViewController:_loginVC animated:YES];
        
    }else{
        //    TestViewController *testVC = [[TestViewController alloc]initWithNibName:@"TestViewController" bundle:nil];
        //    [self presentViewController:testVC animated:YES completion:nil];
        
        _personVC = [[HNPersonalHomePageViewController alloc]initWithNibName:@"HNPersonalHomePageViewController" bundle:nil];
        
        // 设置代理 这里的代理是setting 不是用户界面
        
        _personVC.delegate = self;
        
        [self.navigationController pushViewController:_personVC animated:YES];
        
    }

   
}

// 添加用户信息
- (void)addUserInfomatiom{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *userName = [defaults stringForKey:@"name"];
    
    NSString *userIcon = [defaults stringForKey:@"iconURL"];
    
    if (userName ) {
        
        [_icon sd_setImageWithURL:[NSURL URLWithString:userIcon]];
        
        _firstCellLabel.text = userName;
        
        
    }else{
        
       
         _icon.image = [UIImage imageNamed:@"Comment_Avatar"];
        
        _firstCellLabel.text = @"我的资料";
        
    }
    
}

#pragma mark ---- 实现_loginVC的代理
// 登录成功之后的方法
- (void)loginViewControllerDidFinishLogin{
    
     [MBProgressHUD showSuccess:@"登陆成功"];
    
    // 改变用户界面用户信息
    if (_delegate && [_delegate respondsToSelector:@selector(userSettingViewControllerNeedChangeUserInfamatiom)]) {
        
        // 只有一个代理不需要判断是谁执行这个操作
        [_delegate userSettingViewControllerNeedChangeUserInfamatiom];
        
    }
    
    // 改变设置界面的用户信息
    
    [self addUserInfomatiom];
    
    
}

#pragma mark --- 实现_personVC的代理方法
// 登录成功之后实现的方法
- (void)personalHomePageViewControllerRemoveLogin{
    
   // 由于代理是setting 所以想要改变用户界面的登录状态 需要用代理进行传值
    // 改变用户界面用户信息
    if (_delegate && [_delegate respondsToSelector:@selector(userSettingViewControllerNeedChangeUserInfamatiom)]) {
        
        // 只有一个代理不需要判断是谁执行这个操作
        [_delegate userSettingViewControllerNeedChangeUserInfamatiom];
        
    }

    // 改变设置界面的用户信息
    
    [self addUserInfomatiom];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
